package com.fau.cm.action.index;


import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallEnd;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  中心话务管理已结束任务Action
 * @author yangzj
 * @datetime 2014年10月14日,上午10:39:10
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallManageCenterEndAction extends PublicAction implements ModelDriven<MedicalCallEnd>{
	
	private static final long serialVersionUID = 6349279192012513543L;
	
	private MedicalCallEnd callEnd = new MedicalCallEnd();
	
	/**
	 * @desc 跳转话务任务管理--已结束页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月24日,下午10:45:23
	 */
	@Action(value = "call_manage_center_end_init", results = {@Result(location = "call_manage_center_end_init.jsp")})
	public String init(){
		return SUCCESS;
	}

	/**
	 * @desc 分页查询话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月15日,下午4:46:43
	 */
	@Action(value = "call_manage_center_end_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getCallCenterManagerService().findCallEndByPage(callEnd, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 回收任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月29日,下午5:48:01
	 */
	@Action(value = "call_manage_center_end_recover")
	public String recover() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callEnd.getId())) {
			this.baseAllService.getCallCenterManagerService().recoverEndCall(callEnd.getId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月23日,下午2:07:46
	 */
	@Action(value = "call_manage_center_end_cat")
	public String editCategory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callEnd.getId()) && StringUtils.isNotBlank(callEnd.getCategory())){
			this.baseAllService.getCallCenterManagerService().editCallEndCategory(callEnd.getId(), callEnd.getCategory());
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	
	/**
	 * @desc 批量修改任务状态
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月25日,下午4:59:10
	 */
	@Action(value = "call_manage_center_end_sta")
	public String editStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callEnd.getId()) && StringUtils.isNotBlank(callEnd.getPocessingStatus())) {
			this.baseAllService.getCallCenterManagerService().editCallEndSta(callEnd.getId(), callEnd.getPocessingStatus());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月19日,下午7:25:29
	 */
	@Action(value = "call_manage_center_end_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callEnd.getId())){
			this.baseAllService.getCallCenterManagerService().delEndCall(callEnd);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	

	/**
	 * @desc 导出全部-已结束
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午4:25:55
	 */
	@Action(value = "call_manage_center_end_export_all")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心已结束话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCallCenterManagerService().endExportAll(admin,this.callEnd);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前-已结束
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午4:38:55
	 */
	@Action(value = "call_manage_center_end_export_cur")
	public String exportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心已结束话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			if(StringUtils.isNotBlank(callEnd.getId())){
				Workbook workbook = this.baseAllService.getCallCenterManagerService().endExportCurrent(callEnd.getId());
				workbook.write(toClient);
			}
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public MedicalCallEnd getModel() {
		return callEnd;
	}
	

}
